package com.github.caijh.mall.store.controller;

import com.github.caijh.commons.util.Strings;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.common.CommonPage;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.common.PageParams;
import com.github.caijh.mall.store.model.StoreProductRule;
import com.github.caijh.mall.store.request.StoreProductRuleRequest;
import com.github.caijh.mall.store.request.StoreProductRuleSearchRequest;
import com.github.caijh.mall.store.service.StoreProductRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("api/admin/store/product/rule")
@Api(tags = "商品 -- 规则值(规格)")
public class StoreProductRuleController extends BaseController {

    @Autowired
    private StoreProductRuleService storeProductRuleService;

    /**
     * 分页显示商品规则值(规格)表
     *
     * @param request    搜索条件
     * @param pageParams 分页参数
     * @return StoreProductRule Page
     */
    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<StoreProductRule>> getList(
            @Validated StoreProductRuleSearchRequest request, @Validated PageParams pageParams) {
        CommonPage<StoreProductRule> storeProductRuleCommonPage =
                CommonPage.restPage(storeProductRuleService.getList(request, pageParams));
        return CommonResult.success(storeProductRuleCommonPage);
    }

    /**
     * 新增商品规则值(规格)表
     *
     * @param storeProductRuleRequest 新增参数
     */
    @ApiOperation(value = "新增")
    @PostMapping(value = "/save")
    public void save(@RequestBody @Validated StoreProductRuleRequest storeProductRuleRequest) {
        storeProductRuleService.save(storeProductRuleRequest);
    }

    /**
     * 删除商品规则值(规格)表
     *
     * @param ids 规格ids
     */
    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete/{ids}")
    public void delete(@PathVariable String ids) {
        storeProductRuleService.removeByIds(Strings.toIntListByComma(ids));
    }

    /**
     * 修改商品规则值(规格)表
     *
     * @param storeProductRuleRequest 修改参数
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public void update(@RequestBody @Validated StoreProductRuleRequest storeProductRuleRequest) {
        StoreProductRule storeProductRule = new StoreProductRule();
        BeanUtils.copyProperties(storeProductRuleRequest, storeProductRule);
        storeProductRule.setId(storeProductRuleRequest.getId());
        storeProductRuleService.updateById(storeProductRule);
    }

    /**
     * 查询商品规则值(规格)表信息
     *
     * @param id Integer
     * @return StoreProductRule
     */
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info/{id}")
    public StoreProductRule info(@PathVariable Integer id) {
        return storeProductRuleService.getById(id);
    }

}



